package com.ys.utils.img;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.util.ImageUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

/**
 * @author quxinjun
 * @version 1.0.0
 * @ClassName ImgUtils.java
 * @Description TODO
 * @createTime 2021年05月19日 19:25:00
 */
@Slf4j
public class ImgUtils {

    /**
     * 读取远程url图片,得到宽高
     *
     * @param imgurl 图片路径
     * @return [0] 宽  [1]高
     */
    public static int[] getImgWH(String imgurl) {
        boolean b = false;
        try {
            //实例化url
            URL url = new URL(imgurl);
            //载入图片到输入流
            java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
            //实例化存储字节数组
            byte[] bytes = new byte[100];
            //设置写入路径以及图片名称
            OutputStream bos = new FileOutputStream(new File("pic.jpg"));
            int len;
            while ((len = bis.read(bytes)) > 0) {
                bos.write(bytes, 0, len);
            }
            bis.close();
            bos.flush();
            bos.close();
            //关闭输出流
            b = true;
        } catch (Exception e) {
            //如果图片未找到
            b = false;
        }
        int[] a = new int[2];
        if (b) {//图片存在
            //得到文件
            java.io.File file = new java.io.File("pic.jpg");
            BufferedImage bi = null;
            boolean imgwrong = false;
            try {
                //读取图片
                bi = javax.imageio.ImageIO.read(file);
                try {
                    //判断文件图片是否能正常显示,有些图片编码不正确
                    int i = bi.getType();
                    imgwrong = true;
                } catch (Exception e) {
                    imgwrong = false;
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (imgwrong) {
                a[0] = bi.getWidth(); //获得 宽度
                a[1] = bi.getHeight(); //获得 高度
            } else {
                a = null;
            }
            //删除文件
            file.delete();
        } else {//图片不存在
            a = null;
        }
        return a;
    }


    /**
     * @throws
     * @Title: modifyImagetogeter
     * @Description: 将几张图片合成一张图片
     * @param: @param b1
     * @param: @param b2
     * @param: @param b3
     * @param: @return
     * @return: BufferedImage
     * @author:石德斌
     * @date:2020年4月21日 下午3:26:41
     */
    public static BufferedImage modifyImagetogeter(BufferedImage[] image) {
        //创建一个304*304的图片
        BufferedImage tag = new BufferedImage(304, 304, BufferedImage.TYPE_INT_RGB);
        try {
            Graphics2D graphics = tag.createGraphics();
            //设置颜色为218，223，224
            graphics.setColor(new Color(218, 223, 224));
            //填充颜色
            graphics.fillRect(0, 0, 304, 304);
            int count = image.length;
            //根据不同的合成图片数量设置图片放置位置
            if (count == 1) {
                int startX = 108;
                int startY = 108;
                BufferedImage b = image[0];
                graphics.drawImage(b, startX, startY, b.getWidth(), b.getHeight(), null);
            } else if (count == 2) {
                int startX = 60;
                int startY = 108;
                BufferedImage b1 = image[0];
                graphics.drawImage(b1, startX, startY, b1.getWidth(), b1.getHeight(), null);
                BufferedImage b2 = image[1];
                startX = startX + b1.getWidth() + 8;
                graphics.drawImage(b2, startX, startY, b2.getWidth(), b2.getHeight(), null);
            } else if (count == 3) {
                int startX = 108;
                int startY = 60;
                BufferedImage b1 = image[0];
                graphics.drawImage(b1, startX, startY, b1.getWidth(), b1.getHeight(), null);
                BufferedImage b2 = image[1];
                startX = 60;
                startY = 60 + b1.getHeight() + 8;
                graphics.drawImage(b2, startX, startY, b2.getWidth(), b2.getHeight(), null);
                BufferedImage b3 = image[2];
                startX = startX + b2.getWidth() + 8;
                graphics.drawImage(b3, startX, startY, b3.getWidth(), b3.getHeight(), null);
            } else if (count == 4) {
                int startX = 60;
                int startY = 60;
                BufferedImage b1 = image[0];
                graphics.drawImage(b1, startX, startY, b1.getWidth(), b1.getHeight(), null);
                BufferedImage b2 = image[1];
                startX = 60 + b1.getWidth() + 8;
                graphics.drawImage(b2, startX, startY, b2.getWidth(), b2.getHeight(), null);
                BufferedImage b3 = image[2];
                startX = 60;
                startY = 60 + b2.getHeight() + 8;
                graphics.drawImage(b3, startX, startY, b3.getWidth(), b3.getHeight(), null);
                BufferedImage b4 = image[3];
                startX = 60 + b3.getWidth() + 8;
                graphics.drawImage(b4, startX, startY, b4.getWidth(), b4.getHeight(), null);
            } else if (count == 5) {
                int startX = 60;
                int startY = 60;
                BufferedImage b1 = image[0];
                graphics.drawImage(b1, startX, startY, b1.getWidth(), b1.getHeight(), null);
                BufferedImage b2 = image[1];
                startX = startX + b1.getWidth() + 8;
                graphics.drawImage(b2, startX, startY, b2.getWidth(), b2.getHeight(), null);
                startX = 12;
                startY = 12 + startY + b2.getHeight();
                for (int i = 2; i < count; i++) {
                    BufferedImage b = image[i];
                    graphics.drawImage(b, startX, startY, b.getWidth(), b.getHeight(), null);
                    startX = startX + b.getWidth() + 8;
                }
            } else if (count == 6) {
                int startX = 12;
                int startY = 60;
                for (int i = 0; i < count; i++) {
                    BufferedImage b = image[i];
                    graphics.drawImage(b, startX, startY, b.getWidth(), b.getHeight(), null);
                    startX = startX + b.getWidth() + 8;
                    if ((i + 1) % 3 == 0) {
                        startY = startY + b.getHeight() + 8;
                        startX = 12;
                    }
                }
            } else if (count == 7) {
                int startX = 108;
                int startY = 12;
                BufferedImage b = image[0];
                graphics.drawImage(b, startX, startY, b.getWidth(), b.getHeight(), null);
                startX = 12;
                startY = startY + 8 + b.getHeight();
                for (int i = 1; i < count; i++) {
                    b = image[i];
                    graphics.drawImage(b, startX, startY, b.getWidth(), b.getHeight(), null);
                    startX = startX + b.getWidth() + 8;
                    if (i % 3 == 0) {
                        startY = startY + b.getHeight() + 8;
                        startX = 12;
                    }
                }
            } else if (count == 8) {
                int startX = 60;
                int startY = 12;
                BufferedImage b1 = image[0];
                graphics.drawImage(b1, startX, startY, b1.getWidth(), b1.getHeight(), null);
                BufferedImage b2 = image[1];
                startX = startX + b1.getWidth() + 8;
                graphics.drawImage(b2, startX, startY, b2.getWidth(), b2.getHeight(), null);
                startX = 12;
                startY = 12 + b2.getHeight() + 8;
                for (int i = 2; i < count; i++) {
                    BufferedImage b = image[i];
                    graphics.drawImage(b, startX, startY, b.getWidth(), b.getHeight(), null);
                    startX = startX + b.getWidth() + 8;
                    if (i == 4) {
                        startY = startY + b.getHeight() + 8;
                        startX = 12;
                    }
                }
            } else if (count == 9) {
                int startX = 12;
                int startY = 12;
                for (int i = 0; i < count; i++) {
                    BufferedImage b = image[i];
                    graphics.drawImage(b, startX, startY, b.getWidth(), b.getHeight(), null);
                    startX = startX + b.getWidth() + 8;
                    if ((i + 1) % 3 == 0) {
                        startY = startY + b.getHeight() + 8;
                        startX = 12;
                    }
                }
            }
            graphics.dispose();
        } catch (Exception e) {
            log.error("推送同比压缩图片出错{}", e);
        }

        return tag;
    }

    public static BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
//        	return ImageIO.read(new URL(imgName));
        } catch (IOException e) {
            log.error("推送同比压缩图片出错{}", e);
        }
        return null;
    }

    public static void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
                log.error("推送同比压缩图片出错{}", e);
            }
        }
    }

    /**
     * @throws
     * @Title: handleLarge
     * @Description: 同比例压缩图片，使图片形成相同大小
     * @param: @param image
     * @param: @return
     * @return: BufferedImage[]
     * @author:石德斌
     * @date:2020年4月23日 上午10:36:11
     */
    private static BufferedImage[] handleLarge(Integer width, Integer height, BufferedImage[] image) {
        BufferedImage[] b = new BufferedImage[image.length];
        for (int i = 0; i < image.length; i++) {
            BufferedImage sourceImage = image[i];
            try {
                b[i] = zoom2(width, height, sourceImage);
            } catch (Exception e) {
                log.error("推送同比压缩图片出错{}", e);
            }
        }
        return b;
    }

    public static void main(String[] args) {
//		BufferedImage b1 = loadImageLocal("C:\\Users\\Admin\\Documents\\WeChat Files\\a010662716\\FileStorage\\File\\2020-04\\头像\\");
        try {
            //用于合成的图片数量
            int count = 8;
            BufferedImage[] image = new BufferedImage[count];
            for (int i = 0; i < count; i++) {
                image[i] = loadImageLocal("D:\\桌面\\need\\" + (i + 1) + ".jpg");
            }
            writeImageLocal("D:\\桌面\\need\\result2.jpg", modifyImagetogeter(handleLarge(88, 88, image)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static BufferedImage zoom2(int width, int height, BufferedImage sourceImage) throws Exception {

        if (sourceImage == null) {
            return sourceImage;
        }
        // 计算x轴y轴缩放比例--如需等比例缩放，在调用之前确保參数width和height是等比例变化的
        double ratiox = (new Integer(width)).doubleValue() / sourceImage.getWidth();
        double ratioy = (new Integer(height)).doubleValue() / sourceImage.getHeight();
        AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratiox, ratioy), null);
        BufferedImage bufImg = op.filter(sourceImage, null);
        return bufImg;
    }

}
